<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/6/3
 * Time: 16:30
 */

use Common\Api\BoyeServiceApi;

function mwApi($type,$data){

    $t = array(
        'api_ver'  => 100,
        'notify_id'=> time(),
        'alg'      => 'md5',
        'type'	   => $type
    );



    $data = array_merge($t, $data);

    $service = new BoyeServiceApi();

    $result = $service->callRemote("",$data,false);
    
    return $result;
}

/**
 *取图片地址
 */
function getImgUrl($id,$size=false){
    $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
    $head_url = $PICTURE_REMOTE_URL.$id;
    if($size) $head_url = $head_url.'&size='.$size;
    return $head_url;
}

/**
 * 取用户头像地址
 */
function getHeadUrl($head,$size=false){
    $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
    if(empty($head)){
        $head_url = C('SITE_URL').'/Public/Mengwu/img/common/xlogo.png';
    }else{
        if(intval($head)>0){
            $head_url = $PICTURE_REMOTE_URL.$head;
            if($size) $head_url = $head_url.'&size='.$size;
        }else{
            $head_url = $head;
        }
    }
    return $head_url;
}

/**
 * 生成分页
 */
function paging($now=1,$size=10,$num,$url,$params=[]){
    $params = array_merge(I('get.'),$params);
    if($num<1)return '';
    //计算总页数
    $pageNum = ceil($num / $size);
    $lis = '';
    if($now!=1){
        //上一页按钮
        $params['p'] = $now-1;
        $curl = U($url,$params);
        $lis.= '<li class="item prev"><a href="'.$curl.'"><span>上一页</span></a></li>';
    }else{
        //上一页按钮禁止
        $lis.= '<li class="item prev prev-disabled"><span>上一页</span></li>';
    }
    if($now<=5){
        for($i=1; $i<=7 && $i<=$pageNum; $i++){
            if($now == $i){
                $lis.= '<li class="item active">'.$i.'</li>';
            }else{
                $params['p'] = $i;
                $curl = U($url,$params);
                $lis.= '<li class="item"><a class="num" href="'.$curl.'">'.$i.'</a></li>';
            }
        }
        if($pageNum>7){
            $lis.= '<li class="item dot">...</li>';
        }
    }
    if($now>5){
        $params['p'] = 1;
        $curl_p1 = U($url,$params);
        $params['p'] = 2;
        $curl_p2 = U($url,$params);
        $lis.= '<li class="item"><a class="num" href="'.$curl_p1.'">1</a></li>
                    <li class="item"><a class="num" href="'.$curl_p2.'">2</a></li>
                    <li class="item dot">...</li>';
        for($i=$now-2;$i<=$now+2 && $i<=$pageNum;$i++){
            if($now == $i){
                $lis.= '<li class="item active">'.$i.'</li>';
            }else{
                $params['p'] = $i;
                $curl = U($url,$params);
                $lis.= '<li class="item"><a class="num" href="'.$curl.'">'.$i.'</a></li>';
            }
        }
        if($pageNum>$now+2){
            $lis.= '<li class="item dot">...</li>';

        }
    }
    if($pageNum>$now){
        //下一页按钮
        $params['p'] = $now+1;
        $curl = U($url,$params);
        $lis.='<li class="item next"><a href="'.$curl.'"><span>下一页</span></a></li>';
    }else{
        //下一页按钮禁止
        $lis.='<li class="item next next-disabled"><span>下一页</span></li>';
    }
    $pageHtml = '<div class="m-page">
                        <div class="inner">
                            <ul class="items">'.$lis.'</ul>
                        </div>
                     </div>';
    return $pageHtml;
}